<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="审核信息" prop="authMessage">
        <el-input
          v-model="queryParams.authMessage"
          placeholder="请输入审核信息"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌ID" prop="brandId">
        <el-input
          v-model="queryParams.brandId"
          placeholder="请输入品牌ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="购买数量" prop="buyCount">
        <el-input
          v-model="queryParams.buyCount"
          placeholder="请输入购买数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类路径" prop="categoryPath">
        <el-input
          v-model="queryParams.categoryPath"
          placeholder="请输入分类路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评论数量" prop="commentNum">
        <el-input
          v-model="queryParams.commentNum"
          placeholder="请输入评论数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="成本价格" prop="cost">
        <el-input
          v-model="queryParams.cost"
          placeholder="请输入成本价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计量单位" prop="goodsUnit">
        <el-input
          v-model="queryParams.goodsUnit"
          placeholder="请输入计量单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品视频" prop="goodsVideo">
        <el-input
          v-model="queryParams.goodsVideo"
          placeholder="请输入商品视频"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品好评率" prop="grade">
        <el-input
          v-model="queryParams.grade"
          placeholder="请输入商品好评率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态 0=待审核 1=审核通过 2=审核拒绝" prop="authFlag">
        <el-select v-model="queryParams.authFlag" placeholder="请选择审核状态 0=待审核 1=审核通过 2=审核拒绝" clearable>
          <el-option
            v-for="dict in dict.type.auth_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上架状态  0=上架 1=下架" prop="marketEnable">
        <el-select v-model="queryParams.marketEnable" placeholder="请选择上架状态  0=上架 1=下架" clearable>
          <el-option
            v-for="dict in dict.type.market_enable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="原图路径" prop="original">
        <el-input
          v-model="queryParams.original"
          placeholder="请输入原图路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入商品价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存" prop="quantity">
        <el-input
          v-model="queryParams.quantity"
          placeholder="请输入库存"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售模式 0=零售 1=批发" prop="salesModel">
        <el-select v-model="queryParams.salesModel" placeholder="请选择销售模式 0=零售 1=批发" clearable>
          <el-option
            v-for="dict in dict.type.sales_model"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="店铺ID" prop="storeId">
        <el-input
          v-model="queryParams.storeId"
          placeholder="请输入店铺ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卖点" prop="sellingPoint">
        <el-input
          v-model="queryParams.sellingPoint"
          placeholder="请输入卖点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺分类" prop="shopCategoryPath">
        <el-input
          v-model="queryParams.shopCategoryPath"
          placeholder="请输入店铺分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小图路径" prop="small">
        <el-input
          v-model="queryParams.small"
          placeholder="请输入小图路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品编号" prop="sn">
        <el-input
          v-model="queryParams.sn"
          placeholder="请输入商品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运费模板ID" prop="templateId">
        <el-input
          v-model="queryParams.templateId"
          placeholder="请输入运费模板ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="缩略图路径" prop="thumbnail">
        <el-input
          v-model="queryParams.thumbnail"
          placeholder="请输入缩略图路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下架原因" prop="underMessage">
        <el-input
          v-model="queryParams.underMessage"
          placeholder="请输入下架原因"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="重量" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入重量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺分类路径" prop="storeCategoryPath">
        <el-input
          v-model="queryParams.storeCategoryPath"
          placeholder="请输入店铺分类路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="大图路径" prop="big">
        <el-input
          v-model="queryParams.big"
          placeholder="请输入大图路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品类型  0=实物商品 1=虚拟商品 2=电子卡券" prop="goodsType">
        <el-select v-model="queryParams.goodsType" placeholder="请选择商品类型  0=实物商品 1=虚拟商品 2=电子卡券" clearable>
          <el-option
            v-for="dict in dict.type.goods_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:goods:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:goods:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:goods:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:goods:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="审核信息" align="center" prop="authMessage" />
      <el-table-column label="品牌ID" align="center" prop="brandId" />
      <el-table-column label="购买数量" align="center" prop="buyCount" />
      <el-table-column label="分类路径" align="center" prop="categoryPath" />
      <el-table-column label="评论数量" align="center" prop="commentNum" />
      <el-table-column label="成本价格" align="center" prop="cost" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="计量单位" align="center" prop="goodsUnit" />
      <el-table-column label="商品视频" align="center" prop="goodsVideo" />
      <el-table-column label="商品好评率" align="center" prop="grade" />
      <el-table-column label="商品详情" align="center" prop="intro" />
      <el-table-column label="审核状态 0=待审核 1=审核通过 2=审核拒绝" align="center" prop="authFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.auth_flag" :value="scope.row.authFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="上架状态  0=上架 1=下架" align="center" prop="marketEnable">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.market_enable" :value="scope.row.marketEnable"/>
        </template>
      </el-table-column>
      <el-table-column label="商品移动端详情" align="center" prop="mobileIntro" />
      <el-table-column label="原图路径" align="center" prop="original" />
      <el-table-column label="商品价格" align="center" prop="price" />
      <el-table-column label="库存" align="center" prop="quantity" />
      <el-table-column label="是否为推荐商品" align="center" prop="recommend" />
      <el-table-column label="销售模式 0=零售 1=批发" align="center" prop="salesModel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sales_model" :value="scope.row.salesModel"/>
        </template>
      </el-table-column>
      <el-table-column label="是否自营" align="center" prop="selfOperated" />
      <el-table-column label="店铺ID" align="center" prop="storeId" />
      <el-table-column label="店铺名称" align="center" prop="storeName" />
      <el-table-column label="卖点" align="center" prop="sellingPoint" />
      <el-table-column label="店铺分类" align="center" prop="shopCategoryPath" />
      <el-table-column label="小图路径" align="center" prop="small" />
      <el-table-column label="商品编号" align="center" prop="sn" />
      <el-table-column label="运费模板ID" align="center" prop="templateId" />
      <el-table-column label="缩略图路径" align="center" prop="thumbnail" />
      <el-table-column label="下架原因" align="center" prop="underMessage" />
      <el-table-column label="重量" align="center" prop="weight" />
      <el-table-column label="店铺分类路径" align="center" prop="storeCategoryPath" />
      <el-table-column label="大图路径" align="center" prop="big" />
      <el-table-column label="${comment}" align="center" prop="params" />
      <el-table-column label="商品类型  0=实物商品 1=虚拟商品 2=电子卡券" align="center" prop="goodsType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.goods_type" :value="scope.row.goodsType"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:goods:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:goods:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核信息" prop="authMessage">
          <el-input v-model="form.authMessage" placeholder="请输入审核信息" />
        </el-form-item>
        <el-form-item label="品牌ID" prop="brandId">
          <el-input v-model="form.brandId" placeholder="请输入品牌ID" />
        </el-form-item>
        <el-form-item label="购买数量" prop="buyCount">
          <el-input v-model="form.buyCount" placeholder="请输入购买数量" />
        </el-form-item>
        <el-form-item label="分类路径" prop="categoryPath">
          <el-input v-model="form.categoryPath" placeholder="请输入分类路径" />
        </el-form-item>
        <el-form-item label="评论数量" prop="commentNum">
          <el-input v-model="form.commentNum" placeholder="请输入评论数量" />
        </el-form-item>
        <el-form-item label="成本价格" prop="cost">
          <el-input v-model="form.cost" placeholder="请输入成本价格" />
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="计量单位" prop="goodsUnit">
          <el-input v-model="form.goodsUnit" placeholder="请输入计量单位" />
        </el-form-item>
        <el-form-item label="商品视频" prop="goodsVideo">
          <el-input v-model="form.goodsVideo" placeholder="请输入商品视频" />
        </el-form-item>
        <el-form-item label="商品好评率" prop="grade">
          <el-input v-model="form.grade" placeholder="请输入商品好评率" />
        </el-form-item>
        <el-form-item label="商品详情" prop="intro">
          <el-input v-model="form.intro" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="审核状态 0=待审核 1=审核通过 2=审核拒绝" prop="authFlag">
          <el-select v-model="form.authFlag" placeholder="请选择审核状态 0=待审核 1=审核通过 2=审核拒绝">
            <el-option
              v-for="dict in dict.type.auth_flag"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上架状态  0=上架 1=下架" prop="marketEnable">
          <el-radio-group v-model="form.marketEnable">
            <el-radio
              v-for="dict in dict.type.market_enable"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="商品移动端详情" prop="mobileIntro">
          <el-input v-model="form.mobileIntro" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="原图路径" prop="original">
          <el-input v-model="form.original" placeholder="请输入原图路径" />
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入商品价格" />
        </el-form-item>
        <el-form-item label="库存" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="销售模式 0=零售 1=批发" prop="salesModel">
          <el-select v-model="form.salesModel" placeholder="请选择销售模式 0=零售 1=批发">
            <el-option
              v-for="dict in dict.type.sales_model"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="店铺ID" prop="storeId">
          <el-input v-model="form.storeId" placeholder="请输入店铺ID" />
        </el-form-item>
        <el-form-item label="店铺名称" prop="storeName">
          <el-input v-model="form.storeName" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="卖点" prop="sellingPoint">
          <el-input v-model="form.sellingPoint" placeholder="请输入卖点" />
        </el-form-item>
        <el-form-item label="店铺分类" prop="shopCategoryPath">
          <el-input v-model="form.shopCategoryPath" placeholder="请输入店铺分类" />
        </el-form-item>
        <el-form-item label="小图路径" prop="small">
          <el-input v-model="form.small" placeholder="请输入小图路径" />
        </el-form-item>
        <el-form-item label="商品编号" prop="sn">
          <el-input v-model="form.sn" placeholder="请输入商品编号" />
        </el-form-item>
        <el-form-item label="运费模板ID" prop="templateId">
          <el-input v-model="form.templateId" placeholder="请输入运费模板ID" />
        </el-form-item>
        <el-form-item label="缩略图路径" prop="thumbnail">
          <el-input v-model="form.thumbnail" placeholder="请输入缩略图路径" />
        </el-form-item>
        <el-form-item label="下架原因" prop="underMessage">
          <el-input v-model="form.underMessage" placeholder="请输入下架原因" />
        </el-form-item>
        <el-form-item label="重量" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入重量" />
        </el-form-item>
        <el-form-item label="店铺分类路径" prop="storeCategoryPath">
          <el-input v-model="form.storeCategoryPath" placeholder="请输入店铺分类路径" />
        </el-form-item>
        <el-form-item label="大图路径" prop="big">
          <el-input v-model="form.big" placeholder="请输入大图路径" />
        </el-form-item>
        <el-form-item label="${comment}" prop="params">
          <el-input v-model="form.params" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品类型  0=实物商品 1=虚拟商品 2=电子卡券" prop="goodsType">
          <el-select v-model="form.goodsType" placeholder="请选择商品类型  0=实物商品 1=虚拟商品 2=电子卡券">
            <el-option
              v-for="dict in dict.type.goods_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="删除状态 0=正常 2=已删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除状态 0=正常 2=已删除" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGoods, getGoods, delGoods, addGoods, updateGoods } from "@/api/product/goods";

export default {
  name: "Goods",
  dicts: ['sales_model', 'auth_flag', 'market_enable', 'goods_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        authMessage: null,
        brandId: null,
        buyCount: null,
        categoryPath: null,
        commentNum: null,
        cost: null,
        goodsName: null,
        goodsUnit: null,
        goodsVideo: null,
        grade: null,
        intro: null,
        authFlag: null,
        marketEnable: null,
        mobileIntro: null,
        original: null,
        price: null,
        quantity: null,
        recommend: null,
        salesModel: null,
        selfOperated: null,
        storeId: null,
        storeName: null,
        sellingPoint: null,
        shopCategoryPath: null,
        small: null,
        sn: null,
        templateId: null,
        thumbnail: null,
        underMessage: null,
        weight: null,
        storeCategoryPath: null,
        big: null,
        params: null,
        goodsType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        authMessage: null,
        brandId: null,
        buyCount: null,
        categoryPath: null,
        commentNum: null,
        cost: null,
        goodsName: null,
        goodsUnit: null,
        goodsVideo: null,
        grade: null,
        intro: null,
        authFlag: null,
        marketEnable: null,
        mobileIntro: null,
        original: null,
        price: null,
        quantity: null,
        recommend: null,
        salesModel: null,
        selfOperated: null,
        storeId: null,
        storeName: null,
        sellingPoint: null,
        shopCategoryPath: null,
        small: null,
        sn: null,
        templateId: null,
        thumbnail: null,
        underMessage: null,
        weight: null,
        storeCategoryPath: null,
        big: null,
        params: null,
        goodsType: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGoods(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGoods(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品编号为"' + ids + '"的数据项？').then(function() {
        return delGoods(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('product/goods/export', {
        ...this.queryParams
      }, `goods_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
